package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void saveWithFlavor(DishDto disDto) {
        this.save(disDto);
        List<DishFlavor> flavors = disDto.getFlavors();
        Long id = disDto.getId();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
            dishFlavorService.saveBatch(flavors);
        }
    }

    @Autowired
    private DishMapper dishMapper;

    @Override
    public Page<DishDto> pageByName(int page, int pageSize, String name) {
        Page<DishDto> pageInfo = new Page<>(page, pageSize);
        dishMapper.pageByName(pageInfo, name);
        return pageInfo;
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(ids!=null,Dish::getId,ids);
        List<Dish> list = this.list(dishLambdaQueryWrapper);
        for (Dish dish : list) {
            Integer status = dish.getStatus();
            if (status==0){
                this.removeById(dish.getId());

            }else {
                throw new CustomException("有商品正在售卖无法删除");

            }
        }
    }

    @Override
    public boolean deleteInSetmeal(List<Long> ids) {
       boolean flag=true;
        LambdaQueryWrapper<SetmealDish> lwq = new LambdaQueryWrapper<>();
        lwq.in(SetmealDish::getSetmealId,ids);
        List<SetmealDish> list = setmealDishService.list(lwq);
        if (list.size()==0){
            this.deleteByIds(ids);
            LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
            lqw.in(DishFlavor::getDishId,ids);
            dishFlavorService.remove(lqw);
            return flag;
        }
        ArrayList<Long> longArrayList = new ArrayList<>();
        for (SetmealDish setmealDish : list) {
            Long setmealId = setmealDish.getSetmealId();
            longArrayList.add(setmealId);
        }
        LambdaQueryWrapper<Setmeal> lqq = new LambdaQueryWrapper<>();
        lqq.in(Setmeal::getId,longArrayList);
        List<Setmeal> setmealList = setmealService.list(lqq);
        for (Setmeal setmeal : setmealList) {
            Integer status = setmeal.getStatus();
            if (status==1){
                flag=false;
            }
        }
        this.deleteByIds(ids);
        LambdaQueryWrapper<DishFlavor> lww = new LambdaQueryWrapper<>();
        lww.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lww);
        return flag;
    }

    @Override
    public void updateWithFlavor(DishDto dishDto) {

        this.updateById(dishDto);


        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }


}